<template>
  <div class="container">
    <GlobalHeader :user="currentUser"></GlobalHeader>
    <Loader v-if="isLoading" text="拼命加载中" background="rgba(0,0,0,0.5)"></Loader>
    <router-view></router-view>
    <footer class="text-center py-4 text-secondary bg-light mt-6" style="margin-top: 20px;">
      <small>
        <ul class="list-inline mb-0">
          <li class="list-inline-item">© 2020 者也专栏</li>
          <li class="list-inline-item">课程</li>
          <li class="list-inline-item">文档</li>
          <li class="list-inline-item">联系</li>
          <li class="list-inline-item">更多</li>
        </ul>
      </small>
    </footer>
  </div>
</template>
<script lang="ts" setup>
import 'bootstrap/dist/css/bootstrap.min.css'
import GlobalHeader from './components/GlobalHeader.vue';
import Loader from './components/Loader.vue';
import { GlobalDataProps } from './utlis/type';
import { useStore } from 'vuex';
import {computed, onMounted, watch} from 'vue'
import httpInstance from './axios';
import createMessage from './components/createMessage';
const store = useStore<GlobalDataProps>()
const currentUser = computed(() => store.state.user)
const isLoading = computed(()=>store.state.loading)
const error = computed(()=>store.state.error)
//获取token的值
const token = computed(()=>store.state.token)
onMounted(()=>{
  //如果用户没有登录但是有token值,则需要设置Authorization的值
  if(!currentUser.value.isLogin && token.value){
    httpInstance.defaults.headers.common.Authorization = `Bearer ${token.value}`
    store.dispatch('fetchCurrentUser')
  }
})
watch(()=>error.value.status,()=>{
  const {status,message} = error.value
  if(status && message){
    createMessage(message,'error')
  }
})
</script>

<style>

</style>